﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using E_Surfing.SFReaderWP8.Model.Http;

namespace Game77SharedLib
{
    public partial class MokaPage : PhoneApplicationPage
    {
        public static string imageAddress = "http://mokaad.qiniudn.com/moka_kp_20141016.jpg";
        public static string linkAddress = "http://bj.adserve.ifreeteam.com:9090/advertise/ad/receive.json?id=c035da4065183dbec79ffbe43fa11301ac619c2b8fdd723b692ae4b7915b3ce7";
        Random r = new Random(DateTime.Now.Millisecond);

        public MokaPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            ////?channle=ppq
            //NavigationContext.QueryString.TryGetValue("imageAddress", out imageAddress);
            //NavigationContext.QueryString.TryGetValue("linkAddress", out linkAddress);

            ReaderImage.GetImageFromNet((BitmapImage tempBitmapImage, string contentId) =>
            {
                this.imgAd.Source = tempBitmapImage;
            }, imageAddress, ReaderImage.GetImageAddressId(imageAddress));

            //开始进行开屏广告倒计时
            this.StoryboardProgress.Completed += StoryboardProgress_Completed;
            this.StoryboardProgress.BeginTime = TimeSpan.FromSeconds(1);
            this.StoryboardProgress.Begin();
        }

        void StoryboardProgress_Completed(object sender, EventArgs e)
        {
            if (this.NavigationService.CurrentSource.ToString().Contains("MokaPage.xaml") 
                && this.NavigationService.CanGoBack)
            {
                this.NavigationService.GoBack();
            }
        }

        private void btnClose_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // 在此处添加事件处理程序实现。
            this.NavigationService.GoBack();
        }

        private void imgAd_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            // 在此处添加事件处理程序实现。
            //跳转到魔卡幻想
            WebBrowserTask webBrowserTask = new WebBrowserTask();
            // old，分游戏渠道
            //webBrowserTask.Uri = new Uri("http://adserve.ifreeteam.com/api/clickcallback.php?adprovider=77studio&game=" + channelName, UriKind.Absolute);
            // 魔卡幻想的链接
            //webBrowserTask.Uri = new Uri("http://bj.adserve.ifreeteam.com:9090/advertise/ad/receive.json?id=c035da4065183dbec79ffbe43fa11301ac619c2b8fdd723b692ae4b7915b3ce7", UriKind.Absolute);
            // 千魔斩的链接
            // webBrowserTask.Uri = new Uri("http://bj.adserve.ifreeteam.com:9090/advertise/ad/receive.json?id=5fc22bb64e05cb93477f153d9d61cb409179c3036db5ba85446dde582ec3c345", UriKind.Absolute);
            // 四国传奇的链接
            //webBrowserTask.Uri = new Uri("http://bj.adserve.ifreeteam.com:9090/advertise/ad/receive.json?id=c8ffe9a587", UriKind.Absolute);

            webBrowserTask.Uri = new Uri(linkAddress, UriKind.Absolute);

            webBrowserTask.Show();
        }
    }
}